package com.innovation.chengzhangriji.ui.widget;

import android.app.DatePickerDialog;
import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.text.format.DateFormat;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.DatePicker;
import android.widget.RelativeLayout;

import com.innovation.chengzhangriji.R;

import java.util.Calendar;

public class SelectDateLayout extends RelativeLayout implements View.OnClickListener {

    private CuteTextView mCtvHint;

    private CuteTextView mCtvText;

    private Calendar calendar;

    private long time;

    public SelectDateLayout(Context context) {
        super(context);
        init(context, null);
    }

    public SelectDateLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public SelectDateLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    @Override
    public void onClick(View v) {
        onCreateDialog();
    }

    public void setCuteHint(String hint) {
        mCtvHint.setText(hint);
    }

    public void setCuteHint(int resid) {
        mCtvHint.setText(resid);
    }

    public void setText(String text) {
        mCtvText.setText(text);
    }

    public long getTime() {
        return time;
    }

    private void init(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.SelectDateLayout);
        String selectDataHint = typedArray.getString(R.styleable.SelectDateLayout_select_date_hint);
        int hintColor = typedArray.getColor(R.styleable.SelectDateLayout_select_date_hint_color, getResources().getColor(android.R.color.darker_gray));
        int textColor = typedArray.getColor(R.styleable.SelectDateLayout_select_date_color,getResources().getColor(android.R.color.black));
        typedArray.recycle();

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.layout_select, this);
        mCtvHint = (CuteTextView) view.findViewById(R.id.cute_text_view_hint);
        mCtvHint.setTextColor(hintColor);
        mCtvText = (CuteTextView) view.findViewById(R.id.cute_text_view_text);
        mCtvText.setTextColor(textColor);
        setOnClickListener(this);

        if (!TextUtils.isEmpty(selectDataHint)) {
            mCtvHint.setText(selectDataHint);
        }

        calendar = Calendar.getInstance();
        time = calendar.getTimeInMillis();
        mCtvText.setText(DateFormat.format("yyyy-MM-dd", calendar.getTime()));
    }

    private void onCreateDialog() {
        new DatePickerDialog(getContext(), new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                calendar.set(year, monthOfYear, dayOfMonth, 0, 0, 0);
                time = calendar.getTimeInMillis();
                mCtvText.setText(DateFormat.format("yyyy-MM-dd", calendar.getTime()));
            }
        }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)).show();
    }
}
